package com.tulele.vtool.handler;

import com.google.common.collect.Maps;
import com.tulele.vtool.model.ArticleInfo;
import com.tulele.vtool.model.BDecorationArticles;
import com.tulele.vtool.model.BFloorPlan;
import com.tulele.vtool.model.UserInfo;
import com.tulele.vtool.model.combinedEntity.BDecorationDto;
import com.tulele.vtool.model.reqEntity.GetMicroStationHomeInfoReqBean;
import com.tulele.vtool.model.respEntity.RespData;
import com.tulele.vtool.model.respEntity.ReturnCode;
import com.tulele.vtool.service.*;
import io.micrometer.core.instrument.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 文章作品详情表(BDecorationArticles)表控制层
 *
 * @author makejava
 * @since 2020-08-05 08:39:12
 */
@Slf4j
@RestController
public class BDecorationArticlesHandler {
    @Autowired
    private IUserBrandInfoService userBrandInfoService;

    @Autowired
    private IUserInfoService userInfoService;
    /**
     * 服务对象
     */
    @Resource
    private BDecorationArticlesService bDecorationArticlesService;

    @Resource
    private BFloorPlanService bFloorPlanService;

    /**
     * 服务对象
     */
    @Autowired
    private SysDictService sysDictService;

    @Autowired
    private IArticleInfoService articleInfoService;


    /**
     * 添加数据
     *
     * @param bDecorationArticles 实例对象
     * @return
     */
    @PostMapping(value = "/addBDecorationArticle", produces = "application/json;charset=UTF-8")
    public RespData addBDecorationArticle(@RequestHeader("userId") String userId, @RequestBody BDecorationArticles bDecorationArticles) {
        RespData respData = new RespData();

        if (null == bDecorationArticles || StringUtils.isBlank(userId)) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        bDecorationArticles.setUserId(Integer.valueOf(userId));
        bDecorationArticles.setCreateTime(new Date());
        bDecorationArticles.setUpdateTime(new Date());
        bDecorationArticles.setWorksFlag(1);
        bDecorationArticles.setShareNum(0);

        this.bDecorationArticlesService.addBDecorationArticle(bDecorationArticles);

        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("添加成功");
        return respData;
    }

    /**
     * 通过userId查询数据
     *
     * @param bDecorationArticles 实例对象
     * @return 实例对象
     */
    @PostMapping(value = "/queryAllList", produces = "application/json;charset=UTF-8")
    public RespData queryAllList(@RequestHeader("userId") String userId, @RequestBody BDecorationArticles bDecorationArticles) {
        RespData respData = new RespData();

        if (null == bDecorationArticles || StringUtils.isBlank(userId)) {
            bDecorationArticles.setUserId(Integer.valueOf(userId));
        }

        bDecorationArticles.setWorksFlag(1);//（0停用 1正常）
        List<BDecorationArticles> bDecorationArticlesService = this.bDecorationArticlesService.queryAllList(bDecorationArticles);

        if (null == bDecorationArticlesService || bDecorationArticlesService.size() == 0) {
            respData.setCode(ReturnCode.NORMAL_SUCC);
            respData.setMsg("暂无数据");
            return respData;
        }

        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("bDecorationArticlesService", bDecorationArticlesService);

        respData.setData(resultMap);
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("查询成功");
        return respData;
    }

    /**
     * 通过id查询数据
     *
     * @param bDecorationArticles 实例对象
     * @return 实例对象
     */
    @PostMapping(value = "/queryOneById", produces = "application/json;charset=UTF-8")
    public RespData queryOneById(@RequestHeader("userId") String userId, @RequestBody BDecorationArticles bDecorationArticles) {
        RespData respData = new RespData();

        if (bDecorationArticles == null || bDecorationArticles.getId() == null) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        BDecorationArticles bDecorationArticlesDto = new BDecorationArticles();
        bDecorationArticlesDto.setId(bDecorationArticles.getId());
        bDecorationArticlesDto.setWorksFlag(1);//（0停用 1正常）
        List<BDecorationArticles> bDecorationArticlesService = this.bDecorationArticlesService.queryOneById(bDecorationArticlesDto);

        BFloorPlan bFloorPlanInfo = new BFloorPlan();
        bFloorPlanInfo.setDecorationId(bDecorationArticles.getId());
        bDecorationArticles.setFloorFlag(1);//（0停用 1正常）
        List<BFloorPlan> bFloorPlanService = this.bFloorPlanService.queryAll(bFloorPlanInfo);

        Integer roomNumberId = sysDictService.selectIdByName(bDecorationArticlesService.get(0).getRoomNumber());
        Integer worksStyleId = sysDictService.selectIdByName(bDecorationArticlesService.get(0).getWorksStyle());
        Integer worksAreaId = sysDictService.selectIdByName(bDecorationArticlesService.get(0).getWorksArea());
        Integer worksPricesId = sysDictService.selectIdByName(bDecorationArticlesService.get(0).getWorksPrices());

        UserInfo userInfo = userInfoService.getUserMainInfo(bDecorationArticlesService.get(0).getUserId());

        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(13);
        resultMap.put("bDecorationArticlesService", bDecorationArticlesService);
        resultMap.put("bFloorPlanService", bFloorPlanService);
        resultMap.put("userId", userInfo.getUserId());
        resultMap.put("avatarUrl", userInfo.getAvatarUrl());
        resultMap.put("nickName", userInfo.getNickName());
        resultMap.put("jobPosition", userInfo.getJobPosition());
        resultMap.put("jobTime", userInfo.getJobTime());
        resultMap.put("signature", userInfo.getSignature());

        resultMap.put("telNum", userInfo.getTelNum());
        resultMap.put("dataQrcodeUrl", userInfo.getQrcodeUrl());

        resultMap.put("roomNumberId", roomNumberId);
        resultMap.put("worksStyleId", worksStyleId);
        resultMap.put("worksAreaId", worksAreaId);
        resultMap.put("worksPricesId", worksPricesId);

        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setData(resultMap);
        respData.setMsg("查询成功");
        return respData;
    }

    /**
     * 通过UserId查询数据
     *
     * @param bDecorationArticles 实例对象
     * @return 实例对象
     */
    @PostMapping(value = "/queryOneByUserId", produces = "application/json;charset=UTF-8")
    public RespData queryOneByUserId(@RequestHeader("userId") String userId, @RequestBody BDecorationArticles bDecorationArticles) {
        RespData respData = new RespData();

        if (bDecorationArticles == null || bDecorationArticles.getId() == null) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        bDecorationArticles.setUserId(Integer.valueOf(userId));
        bDecorationArticles.setWorksFlag(1);//（0停用 1正常）
        List<BDecorationArticles> bDecorationArticlesService = this.bDecorationArticlesService.queryOneById(bDecorationArticles);

        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("bDecorationArticlesService", bDecorationArticlesService);

        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setData(resultMap);
        respData.setMsg("查询成功");
        return respData;
    }

    /**
     * 获取前三篇装修作品
     *
     * @param getMicroStationHomeInfoReqBean 实例对象
     * @return 实例对象
     */
    @PostMapping(value = "/queryAllListThree", produces = "application/json;charset=UTF-8")
    public RespData queryAllListThree(@RequestHeader("userId") String userId, @RequestBody GetMicroStationHomeInfoReqBean getMicroStationHomeInfoReqBean) {
        Integer userIdInt = Integer.valueOf(userId);

        if (getMicroStationHomeInfoReqBean != null && getMicroStationHomeInfoReqBean.getOtherUserId() != null) {
            userIdInt = getMicroStationHomeInfoReqBean.getOtherUserId();
        }

        List<BDecorationArticles> bDecorationArticlesService = this.bDecorationArticlesService.queryAllListBySpecialSize(userIdInt, 3);

        RespData respData = new RespData();
        if (null == bDecorationArticlesService || bDecorationArticlesService.size() == 0) {
            respData.setCode(ReturnCode.NORMAL_SUCC);
            respData.setMsg("暂无数据");
            return respData;
        }

        if (CollectionUtils.isNotEmpty(bDecorationArticlesService)) {
            List<BDecorationDto> collect = bDecorationArticlesService.stream().map(appInfo -> {
                BDecorationDto bDecorationDto = new BDecorationDto();

                UserInfo userInfoDto = this.userInfoService.getUserMainInfo(appInfo.getUserId());

                bDecorationDto.setId(appInfo.getId());
                bDecorationDto.setCoverPlan(appInfo.getCoverPlan());
                bDecorationDto.setHeadLine(appInfo.getHeadLine());
                bDecorationDto.setWorksIntroduce(appInfo.getWorksIntroduce());
                bDecorationDto.setUserId(appInfo.getUserId());
                bDecorationDto.setNickName(userInfoDto.getNickName());
                bDecorationDto.setAvatarUrl(userInfoDto.getAvatarUrl());

                return bDecorationDto;
            }).collect(Collectors.toList());

            Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
            resultMap.put("bDecorationArticlesService", collect);
            respData.setCode(ReturnCode.NORMAL_SUCC);

            respData.setData(resultMap);
            respData.setMsg("成功");
            return respData;
        }
        respData.setCode(ReturnCode.SERVER_ERROR);
        respData.setMsg("服务器异常");
        return respData;
    }

    /**
     * 通过id增加分享数
     *
     * @param bDecorationArticles 实例对象
     * @return 实例对象
     */
    @PostMapping(value = "/countOneById", produces = "application/json;charset=UTF-8")
    public RespData countOneById(@RequestHeader("userId") String userId, @RequestBody BDecorationArticles bDecorationArticles) {
        RespData respData = new RespData();

        if (bDecorationArticles == null || bDecorationArticles.getId() == null) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        bDecorationArticles.setWorksFlag(1);//（0停用 1正常）
        this.bDecorationArticlesService.update(bDecorationArticles);

        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("请求成功");
        return respData;
    }

    /**
     * 通过修改数据
     *
     * @param bDecorationArticles 实例对象/
     * @return 实例对象
     */
    @PostMapping(value = "/updateOneByBDecoration", produces = "application/json;charset=UTF-8")
    public RespData updateOneByBDecoration(@RequestHeader("userId") String userId, @RequestBody BDecorationArticles bDecorationArticles) {
        RespData respData = new RespData();

        if (null == bDecorationArticles || StringUtils.isBlank(userId)) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }
        bDecorationArticles.setUserId(Integer.valueOf(userId));
        this.bDecorationArticlesService.updateOneByBDecoration(bDecorationArticles);

        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("修改成功");
        return respData;
    }


    /**
     * 通过文章id获取前三篇装修作品
     *
     * @param   articleInfo
     * @return 实例对象
     */
    @PostMapping(value = "/queryAllListThreeByArticlId", produces = "application/json;charset=UTF-8")
    public RespData queryAllListThreeByArticlId(@RequestHeader("userId") String userId, @RequestBody ArticleInfo articleInfo) {
        Integer userIdInt = articleInfo.getUserId();

        if (userIdInt == null) {
            userIdInt = Integer.valueOf(userId);
        }

        List<BDecorationArticles> bDecorationArticlesService = this.bDecorationArticlesService.queryAllListBySpecialSize(userIdInt, 3);

        RespData respData = new RespData();
        if (null == bDecorationArticlesService || bDecorationArticlesService.size() == 0) {
            respData.setCode(ReturnCode.NORMAL_SUCC);
            respData.setMsg("暂无数据");
            return respData;
        }

        if (CollectionUtils.isNotEmpty(bDecorationArticlesService)) {
            List<BDecorationDto> collect = bDecorationArticlesService.stream().map(appInfo -> {
                BDecorationDto bDecorationDto = new BDecorationDto();

                UserInfo userInfoDto = this.userInfoService.getUserMainInfo(appInfo.getUserId());

                bDecorationDto.setId(appInfo.getId());
                bDecorationDto.setCoverPlan(appInfo.getCoverPlan());
                bDecorationDto.setHeadLine(appInfo.getHeadLine());
                bDecorationDto.setWorksIntroduce(appInfo.getWorksIntroduce());
                bDecorationDto.setUserId(appInfo.getUserId());
                bDecorationDto.setNickName(userInfoDto.getNickName());
                bDecorationDto.setAvatarUrl(userInfoDto.getAvatarUrl());

                return bDecorationDto;
            }).collect(Collectors.toList());

            Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
            resultMap.put("bDecorationArticlesService", collect);
            respData.setCode(ReturnCode.NORMAL_SUCC);

            respData.setData(resultMap);
            respData.setMsg("成功");
            return respData;
        }

        respData.setCode(ReturnCode.SERVER_ERROR);
        respData.setMsg("服务器异常");
        return respData;
    }
}
